package com.wjyf.reducing_bento.controller;

import com.google.common.util.concurrent.RateLimiter;
import com.wjyf.reducing_bento.common.BaseResponse;
import com.wjyf.reducing_bento.common.ResultUtils;
import com.wjyf.reducing_bento.service.CodeService;
import com.wjyf.reducing_bento.utils.CaffeineUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: lrk
 * @Date: 2023/3/31 下午 1:44
 * @Description:
 */
@RestController
@Api(tags = "发送验证码(未完成)")
@RequestMapping("code")
public class CodeController {

    @Resource
    private CaffeineUtils caffeineUtils;

    @Resource
    private CodeService codeService;

    @GetMapping("put")
    public BaseResponse put(@RequestParam String key, @RequestParam String value) {
        caffeineUtils.putIp(key);
        return ResultUtils.success(null);
    }

    @GetMapping("get")
    public BaseResponse get(@RequestParam String key) {
        RateLimiter value = caffeineUtils.getIp(key);
        return ResultUtils.success(value);
    }


    @ApiOperation("发送验证码")
    @GetMapping("get-code")
    public BaseResponse getCode(@RequestParam @ApiParam("接收验证码的手机号码") String phone, HttpServletRequest request) {
        String code = codeService.getCode(phone, request);
        return ResultUtils.success(code);
    }

}
